/**
 * @(#) CongMoneyMediator.as
 *
 * @date		2009. 03. 25.
 * @version		1.0
 * @author		baek, kwang hyoun
 * @description	건강보험증 신청 - Mediator
 * 				건강보험증을 조회하고 신청하는 Presentation Logic을 관리
 */
package com.skt.common_hr.bankreq.view
{
	import com.skt.common_hr.bankreq.events.gwarekeyEvent;
	import com.skt.common_hr.bankreq.model.BankreqProxy;
	import com.skt.common_hr.bankreq.model.vo.bankreqVO;
	import com.skt.common_hr.bankreq.view.components.BankreqOverViewSac;
	import com.skt.common_hr.bankreq.view.components.ReqBankreqViewSac;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.core.IFlexDisplayObject;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class bankreqMediator extends OverViewMediator
	{
		public static const NAME:String = "bankreqMediator";
		private var _bWaitingSave:Boolean = false;
		private var orgFileName:String = "";

		/**
		* 생성자
		*
		* @param
		* @return void
		*/
        public function bankreqMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }

		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		* 최초 한번만 불림
		*/
 		override protected function initializeView():void
 		{
			super.initializeView();
			setEventListener();
 		}
 		
 		
 		private function setEventListener():void
		{
 			view.addEventListener(BankreqOverViewSac.WRITE_VIEW, onWriteView);
 			view.addEventListener(BankreqOverViewSac.WRITE_MOD, onWriteView);
 		}
 		
 		public function onWriteMod(event:Event):void {
 			onWriteView();
 		}
 		
 		public var _ReqBankreqViewSac:IFlexDisplayObject = null;
 		private function onWriteView(event:Event=null):void
 		{
 			var reqMediator:reqBankreqViewSacMediator;
 				
  			 if(_ReqBankreqViewSac==null) {
  			 	_ReqBankreqViewSac = PopUpManager.createPopUp(view, ReqBankreqViewSac, true);
				reqMediator = new reqBankreqViewSacMediator(_ReqBankreqViewSac);  	
				facade.registerMediator(reqMediator);				 			
 			}
 			else {
 				PopUpManager.addPopUp(_ReqBankreqViewSac, view, true);
  				reqMediator = facade.retrieveMediator(reqBankreqViewSacMediator.NAME) as reqBankreqViewSacMediator; 				
 			}
 			
 			
  			ReqBankreqViewSac(_ReqBankreqViewSac).cbBankType.dataProvider = view.dg_bankList;
  			ReqBankreqViewSac(_ReqBankreqViewSac).txtNum.setFocus();
  			ReqBankreqViewSac(_ReqBankreqViewSac).gubun = (view.tb.selectedIndex + 1).toString();
  			ReqBankreqViewSac(_ReqBankreqViewSac).panelTitle = (view.tb.selectedIndex + 1).toString();
  			
  			ReqBankreqViewSac(_ReqBankreqViewSac).begda = "";
  			ReqBankreqViewSac(_ReqBankreqViewSac).endda = "";
  			
  			ReqBankreqViewSac(_ReqBankreqViewSac).parentObj = view;
  			
  			PopUpManager.centerPopUp(_ReqBankreqViewSac);
  			
  			facade.sendNotification(ReqBankreqViewSac.WRITE_VIEW_RECEIVE, this);
  			
  			// 수정이라면
  			if(event.type == 'WRITE_MOD') {
  				ReqBankreqViewSac(_ReqBankreqViewSac).mode = "MOD";
  				ReqBankreqViewSac(_ReqBankreqViewSac).dataproviders = view.sltdItem;
  			}
  			// 신규라면
  			else {
  				ReqBankreqViewSac(_ReqBankreqViewSac).mode = "INS";  				
  			}
  			
  			if(Variables.company == 'NOS'){
  				_ReqBankreqViewSac.height = 360;
  			}else {
  				_ReqBankreqViewSac.height = 300;
  			}
 			
 		}
 	
		/***
		 * 활성화가 될때마다 불림.
		 */
		override protected function activatedView():void
		{
			super.activatedView();
			clearView();
			var passWordMediator:PassWordCheckMediator = facade.retrieveMediator(PassWordCheckMediator.NAME) as PassWordCheckMediator;
			passWordMediator.execute();
 			loadList();
		}

 		override protected function clearView():void
 		{
 			view.vsCompensation.selectedIndex = 0;
 			view.tb.selectedIndex = 0;
			//loadProfile();
 		}
 		
		override protected function closedView():void
		{
			removeEventListener();
		}


 		private function removeEventListener():void
 		{
//			view.removeEventListener(HthIcFacade.HthIcWrite, onClickNew);
//			view.removeEventListener(gwarekeyEvent.SELECT_DATA, onClickReadOnly);
//			view.removeEventListener(gwarekeyEvent.UPDATE_DATA, onClickModify);
//			view.removeEventListener(gwarekeyEvent.DELETE_DATA, onClickDelete);
//			view.removeEventListener(gwarekeyEvent.PRINT_DATA, onStatementView);
 		}


 		private function loadList():void
 		{
			view.loadMainList();
 		}


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		*
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array
		{
			return basicInterestMessages.concat([
					PassWordCheckMediator.VALID_PASSWORD
					// reload
//					ReqMedicalMoneyMediator.CLOSED_REQUEST_VIEW,
//					MedicalMoneyProxy.LOAD_MEDICALMONEY_LIST_COMPLETE,
//					MedicalMoneyProxy.LOAD_MEDICALMONEY_LIST_FAILED
					]);
		}


		/**
		* Noti별로 처리한다.
		*
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName())
			{
				case PassWordCheckMediator.VALID_PASSWORD:
					view.vsCompensation.selectedIndex = 1; 					
					break;				
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
		
 		/**
		 * Proxy에서 DataLoad를 완료한 후 호출됨
		 *     - load된 데이터를 화면에 출력
		 * @param
		 * @return void
		 */
 		private function setListView(list:ArrayCollection):void
 		{
// 			view.medicalMoneyDataList = list;
// 			commonFacade.setStatusMessage("STATUS_LOADED_LIST", [list.length.toString()]);
// 			if(list.length == 0) {
// 				view.showInitialTip();
// 			}
 		}


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////

		/**
		 * 클릭시 단순 조회
		 * @param
		 * @return void
		 */
 		private function onClickReadOnly(event:gwarekeyEvent):void
 		{
			var param:Object = new Object();
 			param.gwarekey  = event.key;
 			param.pernr = event.cpernr;
 			param.name  = Variables.myName;
 			param.write  = false;

 			//executeReqView(param);
 		}

 		/**
		 * 신규 신청 버튼 처리
		 * @param
		 * @return void
		 */
 		private function onClickNew(event:Event):void
 		{
 			var param:Object = new Object();
 			param.pernr 	= Variables.myPernr;
 			param.name  	= Variables.myName;
 			param.gwarekey  = "";
			param.write  	= true;

 			executeReqView(param);
 		}

 		/**
		 * 의료비 수정 버튼 클릭시 실행
		 * @param
		 * @return void
		 */
 		private function onClickModify(event:gwarekeyEvent):void
 		{
			var param:Object = new Object();
 			param.gwarekey  = event.key;
 			param.pernr = event.cpernr;
 			param.name  = Variables.myName;
			param.write  	= true;

 			executeReqView(param);
 		}

 		/**
		 * 신규 신청 및 수정 화면 실행
		 * @param
		 * @return void
		 */
 		private function executeReqView(param:Object):void
 		{
// 			commonFacade.setStatusMessage("");
//			view.vsMain.selectedIndex = 1;
//
// 			var writeMediator:ReqHthIcMediator = facade.retrieveMediator(ReqHthIcMediator.NAME) as ReqHthIcMediator;
// 			if(!writeMediator) {
//				writeMediator = new ReqHthIcMediator(view.hthIcWriteView);
//	 			facade.registerMediator(writeMediator);
//			}
 			//if(writeMediator) facade.sendNotification(HthIcFacade.LOAD_HTHIC_WRITE_INITIAL, param);
 		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
 		private function findCodeIndex(key:String, srcArr:ArrayCollection):int
		{
			for(var i:int=0; i<srcArr.length; i++) {
				if(srcArr[i].data == key) {
					return i;
				}
			}
			return -1;
		}

       private function get view():BankreqOverViewSac
        {
            return viewComponent as BankreqOverViewSac;
        }


	}
}